package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.*;
import cn.gson.crm.model.mapper.OrderMapper;
import cn.gson.crm.model.mapper.ReturngoodsMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReturngoodsService {
    @Autowired
    ReturngoodsMapper returngoodsMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ReturngoodsDao returngoodsDao;
    @Autowired
    ExchangeGoodsDao exchangeGoodsDao;
    @Autowired
    ReplacementDetailsDao replacementDetailsDao;
    @Autowired
    ReturnDetailsDao returnDetailsDao;
    @Autowired
    RefundDao refundDao;
    @Autowired
    WarehousingDao warehousingDao;
    @Autowired
    WaouDetailsDao waouDetailsDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人下的客户
    public Map<String,Object> selectcunstomer(Integer yid, String dept){
        //单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        List<Employee> employeeList=orderMapper.selectLB();
        List<Employee> employeeList2=orderMapper.selectsp(dept);
        List<Customer> customerList=returngoodsMapper.selectcunstomer(yid);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",customerList);
        map.put("tuidanhao","TH"+newDate+result);
        map.put("huandanhao","HH"+newDate+result);
        map.put("rukudanhao","WX"+newDate+result);
        map.put("dingdan","DD"+newDate+result);
        map.put("jinli",employeeList2);
        map.put("lb",employeeList);
      return map;
    };
    //根据客户id查询此客户下所有订单
    public List<Order> selectorder(Integer oid){
        return  returngoodsMapper.selectorder(oid);
    };
    //根据订单id查询订单详情
    public List<OrderDetails> selectodte(Integer odet){
        return  returngoodsMapper.selectodte(odet);
    };
    //新增退货
    public void savereturngoods(HttpSession session,ReturnGoods returnGoods){
            if(returnGoods.getReturnId()>0){
                returngoodsDao.save(returnGoods);
                returngoodsMapper.updateoder(Math.toIntExact((Long) returnGoods.getOrderByOrderId().getOrderId()));
            }else {
                returngoodsDao.save(returnGoods);
                Check check = new Check();
                User login = (User)session.getAttribute("login");
                int yname= (int) login.getEmployees().get(0).getYgId();
                check.setChecksqinName((long) yname);
                check.setCheckType("退货审批");
                check.setChecktypeid(returnGoods.getReturnId());
                check.setCheckDate(returnGoods.getReturnTime());
                check.setCheckName(returnGoods.getEreApprover());
                check.setCheckCondition("未审核");
                returnGoods.getReturnDetailsList().forEach(p->{
                    p.setReturnGoods(returnGoods);
                });
                Refund refund=new Refund(0,(long)returnGoods.getRetrunMoney(),(long) returnGoods.getRetrunNumbers(),returnGoods.getCustomerByCustomerId(),returnGoods, returnGoods.getReturnTime(),returnGoods.getReturnChager());
                refundDao.save(refund);
                returnDetailsDao.saveAll(returnGoods.getReturnDetailsList());
                returngoodsMapper.updateoder(Math.toIntExact((Long) returnGoods.getOrderByOrderId().getOrderId()));
            }
    }
    //新增换货
    public void saveExchagegoods(HttpSession session,ExchangeGoods exchange){
        System.out.println(exchange.getExcId()+"-------1111-------");
        if(exchange.getExcId()>0){
            returngoodsMapper.updatchage(exchange);
            returngoodsMapper.updateoder2(Math.toIntExact((Long) exchange.getOrderByOrderId().getOrderId()));
        }else {
            exchangeGoodsDao.save(exchange);
            Check check = new Check();
            User login = (User)session.getAttribute("login");
            int yname= (int) login.getEmployees().get(0).getYgId();
            check.setChecksqinName((long) yname);
            check.setCheckType("换货审批");
            check.setChecktypeid((int) exchange.getExcId());
            check.setCheckDate(exchange.getExcTime());
            check.setCheckName(exchange.getEreApprover());
            check.setCheckCondition("未审核");
            exchange.getReplacementDetails().forEach(p->{
                p.setExchangeGoods(exchange);
            });

            replacementDetailsDao.saveAll(exchange.getReplacementDetails());
            returngoodsMapper.updateoder2(Math.toIntExact((Long) exchange.getOrderByOrderId().getOrderId()));
        }
    }
    //查询当前登录人下所有的退货记录
    public  Map<String,Object> selectruet(HttpSession session,Integer rid,Integer page, Integer size){
        rid=fouyuan(session);
        List<ReturnGoods> returnGoods2=returngoodsMapper.selectruet(rid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<ReturnGoods> returnGoods=returngoodsMapper.selectruet(rid);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",returnGoods);
        map.put("tong",returnGoods2);
        return map;
    };
    //退货多条件查询
    public  Map<String,Object> selectwhere(HttpSession session,Integer rid,String where,Integer page, Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<ReturnGoods> returnGoods=returngoodsMapper.selectwhere(rid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",returnGoods);
        return map;
    };
    //根据时间查询
    public  Map<String,Object> selecttime(HttpSession session,Integer rid, String statr, String endtime,Integer page, Integer size){
        rid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<ReturnGoods> returnGoods=returngoodsMapper.selecttime(rid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",returnGoods);
        return map;
    };
    //查详表
    public List<ReturnDetails> selectdtat(Integer rid){
        return returngoodsMapper.selectdtat(rid);
    };
    public List<ReturnGoods> selecttuia(Integer rid){
        return  returngoodsMapper.selecttuia(rid);
    };
}
